var fcMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.francecustomer');
var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
var ctYesno = new i18nMessage('../../servlet/CodeTableServlet?name=CtYesno');
var ctProductFamily = new i18nMessage('../../servlet/CodeTableServlet?name=CtFactoryType');
var franceCustomerStore;
var franceCustomerGrid;
var franceCustomerWindow;

var selectFranceCustomer = function(selectFranceCustomerCallback){
    if (!franceCustomerWindow) {
        franceCustomerWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 900,
            height: 605,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'factoryPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="franceCustomerDiv"></div>'
            }]
        });
    }
    franceCustomerWindow.show();
    
    if (franceCustomerGrid){
		franceCustomerStore.reload();
		return;
	}
    franceCustomerStore = new Ext.data.Store({
                    url: '../../servlet/BiFranceCustomerServlet?action=4',
                    
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'id',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'index', 'name', 'department', 
					 'contactRehearsals',
					 'rehearsalsPhone',
					 'rehearsalsEmail',
					 'rehearsalsFax',
					 'rehearsalsMobile',	
					 
					 'contactPurchases',
					 'purchasesPhone',
					 'purchasesEmail',
					 'purchasesFax',
					 'purchasesMobile',	
					 
					 'contactSupplies', 
					 'suppliesPhone',
					 'suppliesEmail',
					 'suppliesFax',
					 'suppliesMobile',
					 
					 'contactLogistics', 
					 'logisticsPhone',
					 'logisticsEmail',
					 'logisticsFax',
					 'logisticsMobile',
					 
					 'contactQuality', 
					 'qualityPhone',
					 'qualityEmail',
					 'qualityFax',
					 'qualityMobile',
					 
					 'contactInvoice',
					 'invoicePhone',
					 'invoiceEmail',
					 'invoiceFax',
					 'invoiceMobile',
					 
					 'addressBill',
					 //'addressDelivery',
					 'addressRehearsals',
					 'addressProduct',
					 
					 'isPrDiscount',
					 'prDiscount',
					 'isProductionAdvancePayment',
					 'productionAdvancePayment',
					 'isProductionGoodsDeparture',
					 'productionGoodsDeparture',
					 'isProductionLcPayment',
					 'productionLcPayment',
					 'isProductionDiscount',
					 'productionDiscount',
					 
					 'remark','id']),
                    
                    remoteSort: true
                });
    
    franceCustomerStore.setDefaultSort('id', 'desc');
    
    function renderSelectItem(value, p, record){
        return String.format('<a href="javascript: selectFranceCustomerCallback(\'{0}\', \'{1}\', \'{2}\');franceCustomerWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', record.data.id, record.data.name, record.data.department);
    }
	
	var franceCustomerFilters = new Ext.grid.GridFilters({
					  filters:[
					    {type: 'string',  dataIndex: 'name'},
					    {type: 'string',  dataIndex: 'department'}
					]});
    // create the grid
    franceCustomerGrid = new Ext.grid.GridPanel({
        title: fcMessage.get('ui.francecustomer.listtitle'),
        store: franceCustomerStore,
        stripeRows: true,
        plugins: [franceCustomerFilters],
        height: 560,
        columns: [{
            header: message.get('message.list.id'),
            width: 20,
            dataIndex: 'index',
            sortable: false
        }, {
            header: fcMessage.get('ui.francecustomer.name'),
            width: 80,
            dataIndex: 'name',
            sortable: true
        }, {
            header: fcMessage.get('ui.francecustomer.department'),
            dataIndex: 'department',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 30,
            dataIndex: 'id',
            sortable: false,
            renderer: renderSelectItem
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'franceCustomerDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: franceCustomerStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    franceCustomerStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}


